﻿using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo
{
    public class 偏移
    {

        public static void Test(ExternalCommandData commandData)
        {

            var doc = commandData.Application.ActiveUIDocument.Document;
            //获取墙List
            FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
            List<Wall> wallList =
            wallCollector.OfCategory(BuiltInCategory.OST_Walls).OfClass(typeof(Wall)).
            ToList().ConvertAll(x => x as Wall);
            //获取墙的基线List
            ElementId wallLevelID = null;
            List<Curve> curveList = new List<Curve>();
            foreach (Wall wall in wallList)
            {
                Curve wallCurve = (wall.Location as LocationCurve).Curve;
                curveList.Add(wallCurve);
                wallLevelID = wall.LevelId;
            }


            //创建模型线
            List<ModelCurve> modelCurveList = new List<ModelCurve>();
            foreach (var curve in curveList)
            {
                Transaction ts1 = new Transaction(doc, "info");
                ts1.Start();
                SketchPlane sktchPlane = SketchPlane.Create(doc, wallLevelID);
                ModelCurve modelCurve = doc.Create.NewModelCurve(curve, sktchPlane);
                ts1.Commit();
                modelCurveList.Add(modelCurve);
            }

            //对modelCurve进行偏移。modelCurve.GeometryCurve是可读可写的，一定要注意
            //另外，还需注意要对modelCurve.GeometryCurve重新进行赋值。
            //此处只是对CUVve进行了偏移，而没重新建立模型线

            //而如果，先对Curve进行偏移，再生成模型线呢？==>试过了，这样就没有关联性。

            foreach (var modelCurve in modelCurveList)
            {
                Transaction ts1 = new Transaction(doc, "info");
                ts1.Start();
                //下面这行一定要注意，如果只写curve.CreateOffset(500 / 304.8, new XYZ(0,0,1));
                //是没有变化的，只是使用了方法，但是并没有给curve重新赋值。
                modelCurve.GeometryCurve =
                modelCurve.GeometryCurve.CreateOffset(500 / 304.5, new XYZ(0, 0, 1));
                ts1.Commit();
            }
        }



        public static void Test2(ExternalCommandData commandData)
        {

            var doc = commandData.Application.ActiveUIDocument.Document;

            var c1 = Line.CreateBound(new XYZ(0,0,0),new XYZ(100,0,0));
            var c2 = Line.CreateBound(new XYZ(100, 0, 0), new XYZ(100, 50, 0));


            // test.Get(sketch, Line.CreateBound(new XYZ(0, 0, 0), new XYZ(100, 0, 0)));


            //创建模型线
            List<ModelCurve> modelCurveList = new List<ModelCurve>();
            Transaction ts1 = new Transaction(doc, "info");
            ts1.Start();
            var test = new SketchPlaneTest();
            SketchPlane sketch = test.CreateSketchPlane(doc, XYZ.BasisZ, XYZ.Zero);
            ModelCurve modelCurve = doc.Create.NewModelCurve(c1, sketch);
            ModelCurve modelCurve2 = doc.Create.NewModelCurve(c2, sketch);

            ts1.Commit();
            modelCurveList.Add(modelCurve);
           // modelCurveList.Add(modelCurve2);

            //对modelCurve进行偏移。modelCurve.GeometryCurve是可读可写的，一定要注意
            //另外，还需注意要对modelCurve.GeometryCurve重新进行赋值。
            //此处只是对CUVve进行了偏移，而没重新建立模型线

            //而如果，先对Curve进行偏移，再生成模型线呢？==>试过了，这样就没有关联性。

            foreach (var modelCurve1 in modelCurveList)
            {
                Transaction ts2 = new Transaction(doc, "info");
                ts2.Start();
                //下面这行一定要注意，如果只写curve.CreateOffset(500 / 304.8, new XYZ(0,0,1));
                //是没有变化的，只是使用了方法，但是并没有给curve重新赋值。
                modelCurve1.GeometryCurve =
                modelCurve1.GeometryCurve.CreateOffset(150, XYZ.BasisX);

                CurveLoop boundary = new CurveLoop();
                Curve curve1 = modelCurve1.GeometryCurve.CreateOffset(100, XYZ.BasisZ);
                Curve curve2 = modelCurve1.GeometryCurve.CreateOffset(-100, XYZ.BasisZ).CreateReversed();
                boundary.Append(curve1);
                boundary.Append(Line.CreateBound(curve1.GetEndPoint(1), curve2.GetEndPoint(0)));
                boundary.Append(curve2);
                boundary.Append(Line.CreateBound(curve2.GetEndPoint(1), curve1.GetEndPoint(0)));



                ts2.Commit();
            }
        }



    }
}
